// 一些常用的工具方法
// 手机号、身份证号中间字符串的保密，只显示头尾
// str需要截取的字符串 start 头的长度 end 尾的长度
function mobileSecrecy(mobile) {
	return mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}

function idCardSecrecy(idCard) {
	return idCard.replace(/^(.{6})(.*)(.{4})$/, '$1****$3');
}
// 价格转变为9.99
function priceSet(price) {
	// console.log('price: ',price);
	let priceArr = (price + '').split('.');
	// console.log('priceArr: ',priceArr);
	if (priceArr[1]) {
		let newPrice = priceArr[1].substring(0, 2)
		return newPrice.length === 2 ? newPrice : newPrice * 10
	} else {
		return '00'
	}
}
// 将价格分割成99.00
function priceSet2(price,type=1) {
	// console.log('price: ',price);
	let priceArr = (price + '').split('.');
	let r = ''
	// console.log('priceArr: ',priceArr);
	if(priceArr.length > 1){
		let price = priceArr[1].length == 1 ? priceArr[1] + '0' : priceArr[1]
		return type==1 ? priceArr[0] : price
	}else{
		return type==1 ? price : '00'
	}
}
// 格式化日期，返回其 yyyy-mm-dd 格式
function parseDateTime(datetime, type, splitor = '-') {
	let d = datetime
	if (typeof d !== 'object') {
		d = new Date(d)
	}
	const year = d.getFullYear()
	const month = d.getMonth() + 1
	const day = d.getDate()
	const hour = d.getHours()
	const minute = d.getMinutes()
	const second = d.getSeconds()
	const date = [year, lessTen(month), lessTen(day)].join(splitor)
	const time = [lessTen(hour), lessTen(minute), lessTen(second)].join(':')
	if (type === "dateTime") {
		return date + ' ' + time
	}
	return date
}
function lessTen(item) {
	return item < 10 ? '0' + item : item
}
function getDateDiff(data) {
	var result;
	//var str = data;
	//将字符串转换成时间格式
	var timePublish = new Date(data);
	var timeNow = new Date();
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var month = day * 30;
	var diffValue = timeNow - timePublish;
	// var diffMonth = diffValue / month;
	// var diffWeek = diffValue / (7 * day);
	var diffDay = diffValue / day;
	var diffHour = diffValue / hour;
	var diffMinute = diffValue / minute;

	if (diffValue < 0) {
		// alert("错误时间");
		result = "错误时间";
	} else if (diffDay > 1) {
		result = timePublish.getFullYear() + "/";
		result += timePublish.getMonth() + "/";
		result += timePublish.getDate();
		// alert(result);
	} else if (diffHour > 1) {
		result = parseInt(diffHour) + "小时前";
	} else if (diffMinute > 1) {
		result = parseInt(diffMinute) + "分钟前";
	} else {
		result = "刚刚";
	}
	return result;
}
// 获取聊天时间（相差300s内的信息不会显示时间）
	function getChatTime(v1,v2){
		v1=v1.toString().length<13 ? v1*1000 : v1;
		v2=v2.toString().length<13 ? v2*1000 : v2;
		if(((parseInt(v1)-parseInt(v2))/1000) > 300){
			return this.gettime(v1);
		}
	}
	// 人性化时间格式
	function gettime(shorttime){
		shorttime=shorttime.toString().length<13 ? shorttime*1000 : shorttime;
		let now = (new Date()).getTime();
		let cha = (now-parseInt(shorttime))/1000;
		
		if (cha < 43200) {
			// 当天
			return this.dateFormat(new Date(shorttime),"{A} {t}:{ii}");
		} else if(cha < 518400){
			// 隔天 显示日期+时间
			return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日 {A} {t}:{ii}");
		} else {
			// 隔年 显示完整日期+时间
			return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD} {A} {t}:{ii}");
		}
	}
	function dateFormat(date, formatStr) {
		let dateObj = {},
		rStr = /\{([^}]+)\}/,
		mons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
		 
		dateObj["Y"] = date.getFullYear();
		dateObj["M"] = date.getMonth() + 1;
		dateObj["MM"] = this.parseNumber(dateObj["M"]);
		dateObj["Mon"] = mons[dateObj['M'] - 1];
		dateObj["D"] = date.getDate();
		dateObj["DD"] = this.parseNumber(dateObj["D"]);
		dateObj["h"] = date.getHours();
		dateObj["hh"] = this.parseNumber(dateObj["h"]);
		dateObj["t"] = dateObj["h"] > 12 ? dateObj["h"] - 12 : dateObj["h"];
		dateObj["tt"] = this.parseNumber(dateObj["t"]);
		dateObj["A"] = dateObj["h"] > 12 ? '下午' : '上午';
		dateObj["i"] = date.getMinutes();
		dateObj["ii"] = this.parseNumber(dateObj["i"]);
		dateObj["s"] = date.getSeconds();
		dateObj["ss"] = this.parseNumber(dateObj["s"]);
	 
		while(rStr.test(formatStr)) {
			formatStr = formatStr.replace(rStr, dateObj[RegExp.$1]);
		}
		return formatStr;
	}
export {
	priceSet,
	priceSet2,
	mobileSecrecy,
	idCardSecrecy,
	parseDateTime,
	getDateDiff,
	getChatTime
}